// ----- JD Web Toon Web Viewer -----
// Engine Javascript File
// Version : 0.7
// Author : Han Jungdong
// Last Modified Date : 2012.07.12
// Last Modified Date : 2013.11.10

var JDWebToonEngine = 
{
	isIE : -1
	,
	MEDIA_TYPE_NONE: 0
	,
	MEDIA_TYPE_SOUND: 1
	,
	MEDIA_TYPE_SWF: 2
	,
	MEDIA_TYPE_VIDEO: 3
	,
	getFlashContent : function( id )
	{
		var flash;
		if( JDWebToonEngine.isIE ){
			flash=document[id];
		}
		else {
			flash=document.getElementById( id );
		}
        return flash;
	}
	,
	
	init : function( jsonDatas, toonContainer )
	{
		JDWebToonEngine.isIE = !(navigator.appName.indexOf("Microsoft") < 0);
		
		//****************************************************************************
		// Setup
		//****************************************************************************
		function addMusicPlayer( toonContainer )
		{
			var _musicContainer			= document.createElement( "div" );
			_musicContainer.id		= "jMusicPlayer";
			toonContainer.appendChild( _musicContainer );
			
			var flashvars 		= {};
			var parameters 		= {};
			parameters.quality 	= 'high';
			parameters.play 	= 'true';
			parameters.LOOP 	= 'false';
			parameters.wmode 	= 'window';
			parameters.scale	= 'noscale';
			parameters.salign 	= 'lt';
			parameters.allowScriptAccess	='always';
		
			swfobject.embedSWF("jdwebtoon/MusicPlayer.swf","jMusicPlayer","200px","40px","11.0.0","",flashvars,parameters);
		}
		addMusicPlayer( toonContainer );
		
		function addSWFPlayer( divContainer, contentURL, id, width, height, version )
		{
			var flashvars 		= {};
			flashvars.src		= contentURL;
			var parameters 		= {};
			parameters.quality 	= 'high';
			parameters.play 	= 'true';
			parameters.LOOP 	= 'false';
			parameters.wmode 	= 'window';
			parameters.scale	= 'noscale';
			parameters.salign 	= 'lt';
			parameters.allowScriptAccess	='always';
			
			if( Number( version ) >= 3 )
			{
				swfobject.embedSWF("jdwebtoon/SWFPlayerAS3.swf", id, width+"px",height+"px","11.0.0","",flashvars,parameters);
			}else{
				swfobject.embedSWF("jdwebtoon/SWFPlayerAS2.swf", id, width+"px",height+"px","11.0.0","",flashvars,parameters);
			}
		}
		function addVideoPlayer( divContainer, contentURL, id, width, height )
		{
			var flashvars 		= {};
			flashvars.src		= contentURL;
			// flashvars.debug		= "true";
			var parameters 		= {};
			parameters.quality 	= 'high';
			parameters.play 	= 'true';
			parameters.LOOP 	= 'false';
			parameters.wmode 	= 'window';
			parameters.scale	= 'noscale';
			parameters.salign 	= 'lt';
			parameters.allowScriptAccess	='always';
			swfobject.embedSWF("jdwebtoon/VideoPlayer.swf", id, width+"px",height+"px","11.0.0","",flashvars,parameters);
		}
		
		
		// Scroll
		var _idPrefix			= "i_";
		var _idPrefixSWF		= "s_";
		var _idPrefixVideo		= "v_";
		
		// Key : element id ( <img> or <flash object> );
		// Value : controll Target ( <flash object> sound or video )
		var _targets		= {};
		var _parseDatas 	= $.parseJSON( jsonDatas );
		var _len 			= _parseDatas.length;
		for( var i=-1; i<=_len; i++ )
		{
			// Add Image Cut
			var _div 		= document.createElement( "div" );
			_div.className 	= "tooncut";
			toonContainer.appendChild( _div );
			
			if( i == -1 || i == _len )
			{
				var _margin 		= document.createElement( "div" );
				_margin.className 	= "margin";
				_div.appendChild( _margin );
				continue;
			}
			
			var _e 			= _parseDatas[ i ];
			_div.id			= _idPrefix+i;
			
			
			var _jDiv 		= $(_div);
			
			if( _e.type == "Image" )
			{
				// insert image
				var _img		= document.createElement( "img" );
				_img.src		= _e.src;
				_img.className	= "image";
				_div.appendChild( _img );
				
				if( _e.sound != undefined )
				{
					// link Sound
					_targets[ _div.id ] 	= { content: _jDiv, mediaType: JDWebToonEngine.MEDIA_TYPE_SOUND, mediaSrc: _e.sound };
				}else{
					_targets[ _div.id ] 	= { content: _jDiv, mediaType: JDWebToonEngine.MEDIA_TYPE_NONE };
				}
			}else if( _e.type == "FlashMovie" )
			{
				// {"src":"source/item_4.swf","type": "FlashMovie","width":"550","height":"400"}
				
				// Add SWF
				var _swapSWFDIV			= document.createElement( "div" );
				_swapSWFDIV.id			= _idPrefixSWF+i;
				_swapSWFDIV.className	= "tooncut";
				_div.appendChild( _swapSWFDIV );
				
				addSWFPlayer( _swapSWFDIV, _e.src, _swapSWFDIV.id, _e.width, _e.height, _e.version );
				
				// link Sound
				_targets[ _div.id ] 	= { content: _jDiv, mediaType: JDWebToonEngine.MEDIA_TYPE_SWF, mediaSrc: _swapSWFDIV.id };
			}else if( _e.type == "Video" )
			{
				// Add SWF
				var _swapVideoDIV			= document.createElement( "div" );
				_swapVideoDIV.id			= _idPrefixVideo+i;
				_swapVideoDIV.className		= "tooncut";
				_div.appendChild( _swapVideoDIV );
				
				addVideoPlayer( _swapVideoDIV, _e.src, _swapVideoDIV.id, _e.width, _e.height );
				
				// link Sound
				_targets[ _div.id ] 	= { content: _jDiv, mediaType: JDWebToonEngine.MEDIA_TYPE_VIDEO, mediaSrc: _swapVideoDIV.id };
			}
			
		}
		
		
		//****************************************************************************
		// Interaction
		//****************************************************************************
		var $w					= $(window);
		var $d					= $(document);
		var _currentContent 	= null;
		function updateScroll( e )
		{
			var _scrollTop 			= $d.scrollTop();
			var _windowHeight		= $w.innerHeight();
			var _marginHeight		= parseFloat( $(".tooncut").css("marginTop").replace( "px", "" ) ) + parseFloat( $(".tooncut").css("marginBottom").replace( "px", "" ) );
			
			
			var _musicContainer			= document.getElementById( "jMusicPlayer" );
			_musicContainer.style.left	= ($w.innerWidth() - _musicContainer.clientWidth) + "px";
			_musicContainer.style.top	= ($w.innerHeight() - _musicContainer.clientHeight+_scrollTop) + "px";
			
			// find middle object
			var _finded = null;
			for( var _id in _targets )
			{
				var _content			= _targets[ _id ].content;
				//var _contentHeight		= _content.height();
				//var _top				= _content.position().top;
				var _middle				= _content.position().top + ( _content.height() + _marginHeight )/2;
				var _offset				= 50;
				if( _middle <= _windowHeight/2+_scrollTop+_offset 
				&& _middle >= _windowHeight/2+_scrollTop-_offset )
				{
					console.log( "_content:",_content );
					_finded = _targets[ _id ];
					break;
				}
			}
			
			if( _finded != null && _currentContent != _finded )
			{
				if( _currentContent != null )
				{
					// _currentContent stop();
					if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_SOUND )
					{
						JDWebToonEngine.getFlashContent( "jMusicPlayer" ).stopMusic();
					}
					else if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_SWF )
					{
						JDWebToonEngine.getFlashContent( _currentContent.mediaSrc ).stopSWF();
					}
					else if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_VIDEO )
					{
						JDWebToonEngine.getFlashContent( _currentContent.mediaSrc ).stopVideo();
					}
				}
				_currentContent = _finded;
				
				// _currentContent play();
				if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_SOUND )
				{
					JDWebToonEngine.getFlashContent( "jMusicPlayer" ).playMusic( _currentContent.mediaSrc );
				}
				else if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_SWF )
				{
					JDWebToonEngine.getFlashContent( _currentContent.mediaSrc ).playSWF();
				}
				else if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_VIDEO )
				{
					JDWebToonEngine.getFlashContent( _currentContent.mediaSrc ).playVideo();
				}
				
				console.log( "Play new Content : ", _currentContent.mediaType );
			}else if( _finded == null && _currentContent != null ) 
			{
				// _currentContent stop();
				if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_SOUND )
				{
					JDWebToonEngine.getFlashContent( "jMusicPlayer" ).stopMusic();
				}
				else if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_SWF )
				{
					JDWebToonEngine.getFlashContent( _currentContent.mediaSrc ).stopSWF();
				}
				else if( _currentContent.mediaType == JDWebToonEngine.MEDIA_TYPE_VIDEO )
				{
					JDWebToonEngine.getFlashContent( _currentContent.mediaSrc ).stopVideo();
				}
				
				console.log( "Stop new Content : ", _currentContent.mediaType );
				_currentContent = null;
			}
		}
		
		//****************************************************************************
		// Event
		//****************************************************************************
		if( JDWebToonEngine.isIE )
		{
			// IE
			window.attachEvent( "onscroll", updateScroll );
			window.attachEvent( "onresize", updateScroll );
		}else{
			// ETC
			window.addEventListener( "scroll", updateScroll );
			window.addEventListener( "resize", updateScroll );
		}
	}
}